﻿using System.Web.Mvc;
using ClothingAggregator.Models;

namespace ClothingAggregator.Controllers
{
    public class HomeController : Controller
    {
        private MyAggregator aggregator;
        public MyAggregator Aggregator
        {
            get
            {
                if (aggregator == null)
                {
                    aggregator = Session["aggregator"] as MyAggregator ?? new MyAggregator();
                }
                return aggregator;
            }
            set { Session["aggregator"] = value; }
        }

        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Props = Aggregator.GetSortableProperties();
            return View();
        }

        [HttpPost]
        public ActionResult Index(string searchSubmit, MySearchData searchData)
        {
            ViewBag.Props = Aggregator.GetSortableProperties();
            if (searchSubmit != null)
            {
                ModelState.Remove("Page");
            }
            Aggregator.Aggregate(searchData, searchSubmit != null);

            ViewBag.Results = Aggregator.Results;
            ViewBag.AllAvailable = Aggregator.AllAvailable;
            ViewBag.PageCount = Aggregator.PageCount;
            Aggregator = Aggregator;
            return View(searchData);
        }
    }
}
